//
//  IGAppVersionManager.m
//  jingdongfang
//
//  Created by BOE on 2017/11/13.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGAppVersionManager.h"
#import <AFNetworking.h>

@implementation IGAppVersionApi

- (BOOL)showLogicErrorToast {
    return YES;
}

- (NSString *)requestUrl {
    return @"sys/apkUpdate.action";
}

@end

@implementation IGAppVersionModel

@end

@implementation IGAppVersionResp

@end

@implementation IGAppVersionManager

+ (void)checkAppVersion {
    //获得APP当前版本号
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = infoDic[@"CFBundleShortVersionString"];
    
    //获取App Store中APP的版本号
    AFHTTPSessionManager *manager  = [AFHTTPSessionManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@", STOREAPPID];
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject[@"results"];
        NSDictionary *dic = array[0];
        NSString *appInStoreVersion = dic[@"version"];
        
        if ([currentVersion floatValue] < [appInStoreVersion floatValue]) {
            [IGAppVersionManager forceUpdate:appInStoreVersion];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

+ (void)forceUpdate:(NSString *)appInStoreVersion {
    IGAppVersionApi *appVersionApi = [IGAppVersionApi new];
    appVersionApi.updateVersion = [IGServerConfig sharedConfig].shortVersion;
    [appVersionApi startWithDecodeClass:[IGAppVersionResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGAppVersionResp *resp = responseObject;
            IGAppVersionModel *model = resp.data;
            NSString *forced = model.force;
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([forced intValue] == 1) {//强制升级
                    [UIAlertView bk_showAlertViewWithTitle:model.title message:model.content cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"Go to Update", nil)] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                        NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/%E7%94%BB%E5%B1%8F/id1150322560?mt=8"];
                        [[UIApplication sharedApplication] openURL:url];
                    }];
                } else {//非强制升级
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"yyyyMMdd"];

                    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                    NSString *dateStr = [userDefaults objectForKey:@"AppUpdateDate"];
                    if (![dateStr isEqualToString:[dateFormatter stringFromDate:[NSDate date]]]) {//不是同一天才再次提示
                        [UIAlertView bk_showAlertViewWithTitle:model.title message:model.content cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:@[NSLocalizedString(@"Go to Update", nil)] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            if (buttonIndex == 1) {
                                NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/%E7%94%BB%E5%B1%8F/id1150322560?mt=8"];
                                [[UIApplication sharedApplication] openURL:url];

                                NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
                                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                [userDefaults setObject:currentDateStr forKey:@"AppUpdateDate"];
                                [userDefaults synchronize];
                            } else {
                                NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
                                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                [userDefaults setObject:currentDateStr forKey:@"AppUpdateDate"];
                                [userDefaults synchronize];
                            }
                        }];
                    }
                }
            });
        }
    }];
}

@end

